package com.zte.cl.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.zte.cl.service.ContentPageService;
import com.zte.cl.service.SeedPageService;
import com.zte.cl.utils.SpringContextUtil;

/**
 * Servlet implementation class JobServlet
 */
public class JobServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private static final Log log = LogFactory.getLog(JobServlet.class);
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public JobServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		log.info("通过servlet执行抓取任务...");
		
		String method = request.getParameter("method");
		if(method == null || method.trim().equals("") || method.trim().equals("null")) {
			method = "start";
		}
		method = method.trim();
		log.info("method = " + method);
		if(method.equals("start")) {
			log.info("启动种子页抓取任务...");
			SeedPageService seedPageService = (SeedPageService) SpringContextUtil.getBean("seedPageService");
			seedPageService.execute();
			
			log.info("启动内容页抓取任务...");
			ContentPageService contentPageService = (ContentPageService) SpringContextUtil.getBean("contentPageService");
			contentPageService.execute();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
